package com.jdbc.homework2;

import com.jdbc.homework.DataSourceUtils;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * ClassName: LoginSystem
 * Passage: com.jdbc.homework2
 * Description:
 *
 * @Author 余大帅逼
 * @Create 2025/2/26 19:59
 * @Version 1.0
 **/
public class LoginSystem {

    public static boolean login(String username, String password) {
        String sql = "SELECT is_admin FROM users WHERE username = ? AND password = ?";
        try (Connection conn = DataSourceUtils.getConnection();
             PreparedStatement ps = conn.prepareStatement(sql)) {
            ps.setString(1, username);
            ps.setString(2, password);
            try (ResultSet rs = ps.executeQuery()) {
                if (rs.next()) {
                    boolean isAdmin = rs.getBoolean("is_admin");
                    return isAdmin; // 返回用户是否为管理员
                }
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return false; // 登录失败
    }

    public static void showMenu(boolean isAdmin) {
        if (isAdmin) {
            System.out.println("管理员菜单：\n1. 添加宠物\n2. 更新宠物\n3. 删除宠物\n4. 查看宠物");
        } else {
            System.out.println("顾客菜单：\n1. 查看宠物\n2. 购买宠物");
        }
    }
}

